package com.cheetah.controller.protobuf;

import com.cheetah.annotation.ProtobufController;
import com.cheetah.annotation.RequestMapping;
import com.cheetah.annotation.RunExecutor;
import com.cheetah.constants.ProtobufConstants;
import com.cheetah.dto.LoginDTO;
import com.cheetah.dto.UserDTO;
import com.cheetah.protocol.Request;
import com.cheetah.protocol.Response;
import com.cheetah.protocol.ResponseBuilder;
import com.cheetah.protostuff.ProtostuffSerialization;
import com.cheetah.service.LoginOutService;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @ClassName LoginoutHanlder
 * @Description: 登入登出
 * @Author Administrator
 * @Date 2019/7/19
 * @Version V1.0
 **/

@Component
@ProtobufController
@Slf4j
@RunExecutor("login-executor")
public class LoginOutController {

    @Autowired
    private LoginOutService loginOutService;

    @RequestMapping(mapping = ProtobufConstants.LOGIN,auth = false)
    public Response<UserDTO> login(Request request, Channel channel){
        LoginDTO loginDTO = ProtostuffSerialization.deserialize(request.getBody(), LoginDTO.class);
        log.info("user is login! {}",loginDTO);
        loginDTO.setClientId(request.getRequestHeader().getClientId());
        loginDTO.setUserAgent(request.getRequestHeader().getUserAgent());
        loginDTO.setRemoteIp(request.getRequestHeader().getRemoteIp());
        UserDTO userDTO = loginOutService.login(loginDTO,channel);
        log.info("" + userDTO);
        return ResponseBuilder.builder()
                .success(userDTO);
    }

    @RequestMapping(mapping = ProtobufConstants.LOGOUT)
    public Response<UserDTO> logout(Request request){
        UserDTO userDTO = ProtostuffSerialization.deserialize(request.getBody(),UserDTO.class);
        return ResponseBuilder.builder()
                .success(userDTO);
    }
}
